//
//  PayUp.m
//  ProjectPublic
//
//  Created by ac-hu on 2018/7/13.
//  Copyright © 2018年 ac hu. All rights reserved.
//

#import "PayUp.h"
#import "UPPaymentControl.h"

#ifdef DEBUG
#define upPayMode @"00"
#else
#define upPayMode @"01"
#endif

@interface PayUp()
@property(nonatomic,copy)NSString *appScheme;
@property(nonatomic,copy)succeedPay succeedBlock;
@property(nonatomic,copy)failPay faiBlock;
@end

@implementation PayUp

+(instancetype)share{
    static PayUp *pay;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (pay == nil) {
            pay = [[PayUp alloc]init];
            [pay configruation];
        }
    });
    return pay;
}

-(void)configruation{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
    NSDictionary * dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    NSArray * URLTypes = dict[@"CFBundleURLTypes"];
    
    for (NSDictionary * dic in URLTypes) {
        if ([[dic objectForKey:@"CFBundleURLName"] isEqualToString:@"uppay"]) {
            NSArray * URLSchemes= [dic objectForKey:@"CFBundleURLSchemes"];
            self.appScheme = URLSchemes.firstObject;
            break;
        }
    }
}


-(void)payOrder:(id)order succeed:(succeedPay)succeed fail:(failPay)fail{
    NSString *tn = order;
    self.succeedBlock = succeed;
    self.faiBlock = fail;
    [[UPPaymentControl defaultControl] startPay:tn fromScheme:self.appScheme mode:upPayMode viewController:nil];
}

-(void)monitorResult:(NSURL *)url{
    __weak typeof(self) weekSelf = self;
    [[UPPaymentControl defaultControl] handlePaymentResult:url completeBlock:^(NSString *code, NSDictionary *data) {
        if([code isEqualToString:@"success"]) {
            //如果想对结果数据验签，可使用下面这段代码，但建议不验签，直接去商户后台查询交易结果
            if(data != nil){
                //数据从NSDictionary转换为NSString
                NSData *signData = [NSJSONSerialization dataWithJSONObject:data
                                                                   options:0
                                                                     error:nil];
                NSString *sign = [[NSString alloc] initWithData:signData encoding:NSUTF8StringEncoding];
                
                //此处的verify建议送去商户后台做验签，如要放在手机端验，则代码必须支持更新证书
                if([weekSelf verify:sign]) {
                    if (weekSelf.succeedBlock) {
                        //验签成功
                        weekSelf.succeedBlock(@"支付成功");
                    }
                }
                else {
                    //验签失败
                    if (self.faiBlock) {
                        self.faiBlock(@"验签失败");
                    }
                }
            }
            
            //结果code为成功时，去商户后台查询一下确保交易是成功的再展示成功
        }
        else if([code isEqualToString:@"fail"]) {
            //交易失败
            if (self.faiBlock) {
                self.faiBlock(@"交易失败");
            }
        }
        else if([code isEqualToString:@"cancel"]) {
            //交易取消
            if (self.faiBlock) {
                self.faiBlock(@"交易取消");
            }
        }
    }];
}

-(BOOL)verify:(NSString *) resultStr {
    //此处的verify，商户需送去商户后台做验签
    return NO;
}

@end
